{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from pprint import pprint as p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 创建阵列:\n",
    "a = np.arange(10,0,-1)\n",
    "b = np.arange(48).reshape(3,4,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 阵列索引生成的是深副本\n",
    "a[[0,1,2]].flags.owndata"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 结果阵列的数据类型来自原阵列\n",
    "a[[0,1,2]].dtype == a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3,)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "((3, 4, 4), (2, 2, 4, 4))"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 结果阵列的形状是索引阵列的形状串接上未指明的剩余的轴的形状\n",
    "print(a[[0,1,2]].shape)\n",
    "idx = np.array([[0,1],[2,1]])\n",
    "b.shape, b[idx].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "index 101 is out of bounds for axis 1 with size 10",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-6-f99788027941>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 运行以下命令，观察出错信息\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m101\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m: index 101 is out of bounds for axis 1 with size 10"
     ]
    }
   ],
   "source": [
    "# 运行以下命令，观察出错信息\n",
    "a[[0,1,101]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用多个阵列做索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]\n",
      "  [12 13 14 15]]\n",
      "\n",
      " [[16 17 18 19]\n",
      "  [20 21 22 23]\n",
      "  [24 25 26 27]\n",
      "  [28 29 30 31]]\n",
      "\n",
      " [[32 33 34 35]\n",
      "  [36 37 38 39]\n",
      "  [40 41 42 43]\n",
      "  [44 45 46 47]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([ 0, 21])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 当使用多个阵列时，多个阵列对应位置的值组合成索引，运行以下命令，观察输出\n",
    "print(b)\n",
    "b[[0,1],[0,1],[0,1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]\n",
      "  [12 13 14 15]]\n",
      "\n",
      " [[16 17 18 19]\n",
      "  [20 21 22 23]\n",
      "  [24 25 26 27]\n",
      "  [28 29 30 31]]\n",
      "\n",
      " [[32 33 34 35]\n",
      "  [36 37 38 39]\n",
      "  [40 41 42 43]\n",
      "  [44 45 46 47]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [20, 21, 22, 23]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(b)\n",
    "b[[0,1],[0,1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]\n",
      "  [12 13 14 15]]\n",
      "\n",
      " [[16 17 18 19]\n",
      "  [20 21 22 23]\n",
      "  [24 25 26 27]\n",
      "  [28 29 30 31]]\n",
      "\n",
      " [[32 33 34 35]\n",
      "  [36 37 38 39]\n",
      "  [40 41 42 43]\n",
      "  [44 45 46 47]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 8,  9, 10, 11],\n",
       "       [24, 25, 26, 27]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 当阵列后面有数字时，数字将被广播成与前面的阵列相同的形状\n",
    "print(b)\n",
    "b[[0,1],2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "shape mismatch: indexing arrays could not be broadcast together with shapes (3,) (2,2) ",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-10-ebf5e350398b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 当两个阵列形状不同时，尝试广播，无法广播时将出错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m      \u001b[0;31m# <-- (2,)与(2,2)正常广播\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mb\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m    \u001b[0;31m# <-- (3,)与(2,2)无法兼容广播，出错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m: shape mismatch: indexing arrays could not be broadcast together with shapes (3,) (2,2) "
     ]
    }
   ],
   "source": [
    "# 当两个阵列形状不同时，尝试广播，无法广播时将出错\n",
    "b[[0,1],[[1,2],[2,3]]]      # <-- (2,)与(2,2)正常广播\n",
    "b[[0,1,2],[[1,2],[2,3]]]    # <-- (3,)与(2,2)无法兼容广播，出错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]\n",
      "  [12 13 14 15]]\n",
      "\n",
      " [[16 17 18 19]\n",
      "  [20 21 22 23]\n",
      "  [24 25 26 27]\n",
      "  [28 29 30 31]]\n",
      "\n",
      " [[32 33 34 35]\n",
      "  [36 37 38 39]\n",
      "  [40 41 42 43]\n",
      "  [44 45 46 47]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([5, 7])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 索引阵列/切片前面的数据用于按轴的顺序选出指定的数据，供后面的切片/索引阵列索引\n",
    "print(b)\n",
    "b[0,1,[1,3]]\n",
    "#b[0,1,::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]\n",
      "  [12 13 14 15]]\n",
      "\n",
      " [[16 17 18 19]\n",
      "  [20 21 22 23]\n",
      "  [24 25 26 27]\n",
      "  [28 29 30 31]]\n",
      "\n",
      " [[32 33 34 35]\n",
      "  [36 37 38 39]\n",
      "  [40 41 42 43]\n",
      "  [44 45 46 47]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([4, 6])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(b)\n",
    "b[0,1,::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]\n",
      "  [12 13 14 15]]\n",
      "\n",
      " [[16 17 18 19]\n",
      "  [20 21 22 23]\n",
      "  [24 25 26 27]\n",
      "  [28 29 30 31]]\n",
      "\n",
      " [[32 33 34 35]\n",
      "  [36 37 38 39]\n",
      "  [40 41 42 43]\n",
      "  [44 45 46 47]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [32, 33, 34, 35]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片与右边的数字广播时，切片被转成一维阵列，然后再广播\n",
    "print(b)\n",
    "b[::2,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]\n",
      "  [12 13 14 15]]\n",
      "\n",
      " [[16 17 18 19]\n",
      "  [20 21 22 23]\n",
      "  [24 25 26 27]\n",
      "  [28 29 30 31]]\n",
      "\n",
      " [[32 33 34 35]\n",
      "  [36 37 38 39]\n",
      "  [40 41 42 43]\n",
      "  [44 45 46 47]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]],\n",
       "\n",
       "       [[16, 17, 18, 19],\n",
       "        [20, 21, 22, 23],\n",
       "        [24, 25, 26, 27]]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片与切片/阵列广播时，所有切片被转成一维阵列，\n",
    "# 然后左边的转成(X,1)的形状，右边的转成(1,X)的形状，然后广播\n",
    "print(b)\n",
    "b[:2,:3]\n",
    "\n",
    "# [:2,:3] 被转成 [0,1] 和 [0,1,2]\n",
    "# [0,1] 和 [0,1,2] 被转成 [[0],[1]] 和 [[0,1,2]]\n",
    "# [[0],[1]] 和 [[0,1,2]] 被广播成：\n",
    "# np.broadcast_arrays([[0],[1]], np.array([[0,1,2]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]\n",
      "  [12 13 14 15]]\n",
      "\n",
      " [[16 17 18 19]\n",
      "  [20 21 22 23]\n",
      "  [24 25 26 27]\n",
      "  [28 29 30 31]]\n",
      "\n",
      " [[32 33 34 35]\n",
      "  [36 37 38 39]\n",
      "  [40 41 42 43]\n",
      "  [44 45 46 47]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [ 8,  9, 10, 11]],\n",
       "\n",
       "       [[16, 17, 18, 19],\n",
       "        [24, 25, 26, 27]]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(b)\n",
    "b[:2,[0,2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]\n",
      "  [12 13 14 15]]\n",
      "\n",
      " [[16 17 18 19]\n",
      "  [20 21 22 23]\n",
      "  [24 25 26 27]\n",
      "  [28 29 30 31]]\n",
      "\n",
      " [[32 33 34 35]\n",
      "  [36 37 38 39]\n",
      "  [40 41 42 43]\n",
      "  [44 45 46 47]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7]],\n",
       "\n",
       "       [[32, 33, 34, 35],\n",
       "        [36, 37, 38, 39]]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(b)\n",
    "b[[0,2],:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--> [10  9  8]\n",
      "--> [10  9  8]\n"
     ]
    }
   ],
   "source": [
    "# 用能够被转换成阵列的其它数据类型，作用等同于np.array\n",
    "print('-->', a[np.array([0,1,2])])\n",
    "print('-->', a[[0,1,2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--> 6\n",
      "-->\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]\n",
      "  [12 13 14 15]]\n",
      "\n",
      " [[16 17 18 19]\n",
      "  [20 21 22 23]\n",
      "  [24 25 26 27]\n",
      "  [28 29 30 31]]\n",
      "\n",
      " [[32 33 34 35]\n",
      "  [36 37 38 39]\n",
      "  [40 41 42 43]\n",
      "  [44 45 46 47]]]"
     ]
    }
   ],
   "source": [
    "# 但是python的tuple是直接作为索引的，不会被转换成ndarray\n",
    "print('-->', b[(0,1,2)])      # 等价于 b[0,1,2]\n",
    "print('-->', b[np.array((0,1,2))], sep='\\n', end='')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 用bool阵列来索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10,  9,  8,  7,  6,  5,  4,  3,  2,  1])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True,  True, False, False, False, False,\n",
       "       False])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a > 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((10,), (10,))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx = a > 5         # 创建bool阵列\n",
    "idx.shape, a.shape  # 形状相同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "array([10,  9,  8,  7,  6,  5,  4,  3,  2,  1])\n",
      "array([ True,  True,  True,  True,  True, False, False, False, False,\n",
      "       False])\n",
      "array([10,  9,  8,  7,  6])\n",
      "array([10,  9,  8,  7,  6])\n"
     ]
    }
   ],
   "source": [
    "p(a)\n",
    "p(idx)\n",
    "p(a[idx])\n",
    "p(a[:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "array([[[ 0,  1,  2,  3],\n",
      "        [ 4,  5,  6,  7],\n",
      "        [ 8,  9, 10, 11],\n",
      "        [12, 13, 14, 15]],\n",
      "\n",
      "       [[16, 17, 18, 19],\n",
      "        [20, 21, 22, 23],\n",
      "        [24, 25, 26, 27],\n",
      "        [28, 29, 30, 31]],\n",
      "\n",
      "       [[32, 33, 34, 35],\n",
      "        [36, 37, 38, 39],\n",
      "        [40, 41, 42, 43],\n",
      "        [44, 45, 46, 47]]])\n",
      "[(3, 4, 4), (3,)]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11],\n",
       "        [12, 13, 14, 15]]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 手动创建bool阵列\n",
    "p(b)\n",
    "idx = np.array([True, False, False])\n",
    "p([b.shape, idx.shape])\n",
    "b[idx]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 布尔阵列索引另外的阵列时，要求布尔阵列满足以下条件：\n",
    "\n",
    "#### 布尔阵列的shape 与被索引的阵列的shaepe的头部吻合\n",
    "#### 布尔阵列的形状: (2,3), 被索引阵列的形状: (2,3,5,5,5) --> 吻合\n",
    "#### 布尔阵列的形状: (2,3), 被索引阵列的形状: (3,3,5,5,5) --> 不吻合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4) (3, 4, 4)\n",
      "-->\n",
      "[[ True False False False]\n",
      " [ True False False False]\n",
      " [ True False False False]]\n",
      "-->\n",
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]\n",
      "  [ 8  9 10 11]\n",
      "  [12 13 14 15]]\n",
      "\n",
      " [[16 17 18 19]\n",
      "  [20 21 22 23]\n",
      "  [24 25 26 27]\n",
      "  [28 29 30 31]]\n",
      "\n",
      " [[32 33 34 35]\n",
      "  [36 37 38 39]\n",
      "  [40 41 42 43]\n",
      "  [44 45 46 47]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [16, 17, 18, 19],\n",
       "       [32, 33, 34, 35]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx = np.full(b.shape[:2], False, dtype=bool)\n",
    "print(idx.shape, b.shape)      # bool阵列形状与b阵列头部相同\n",
    "idx[:,0] = True\n",
    "\n",
    "print('-->')\n",
    "print(idx)\n",
    "\n",
    "print('-->')\n",
    "print(b)\n",
    "\n",
    "b[idx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "content:\n",
      "[[1 1 1 1]\n",
      " [1 1 1 1]]\n",
      "shape:\n",
      "(2, 4)\n",
      "---\n"
     ]
    }
   ],
   "source": [
    "# 对索引结果赋值，用来赋值的值与索引结果的形状必须相同或者兼容广播\n",
    "print('content:')\n",
    "print(b[[0,2],[0,1]])\n",
    "print('shape:')\n",
    "print(b[[0,2],[0,1]].shape)\n",
    "print('---')\n",
    "\n",
    "b[[0,2],[0,1]] = 0                      # 正常广播\n",
    "b[[0,2],[0,1]] = [1,2,3,4]              # 正常广播\n",
    "b[[0,2],[0,1]] = [[1,2,3,4],[2,3,4,4]]  # 匹配，不需广播\n",
    "# b[[0,2],[0,1]] = [1,2]                  # 不能广播，出错"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
